<?php

namespace app\subscribe\property;


use think\Event;
use think\facade\Db;
use app\common\repositories\energy\EnergyMeterRepository;
use app\common\repositories\property\basic\PropertyLogRepository;
use app\common\repositories\property\auth\PropertyAuthAccessRepository;
use app\common\repositories\property\basic\PropertyBasicLabelRepository;

class PropertySubscribe
{
    /**
     * 创建物业
     * @param $data
     * @return void
     *
     * @author Amos
     * 2024-06-13 14:55
     */
    public function onCreate($data)
    {
        ## 物业信息
        $propertyInfo = $data['propertyInfo'];
        $userId = $data['userId'] ?? '0';

        /** @var PropertyLogRepository $propertyLogRepository */
        $propertyLogRepository = app()->make(PropertyLogRepository::class);
        $propertyLogData['property_id'] = $propertyInfo['id'];
        $propertyLogData['remark'] = '创建物业编号: ' . $propertyInfo['code'];
        $propertyLogRepository->addData($propertyInfo['items_id'], $propertyLogData, $userId);
    }

    /**
     * 编辑物业
     * @param $data
     * @return void
     *
     * @author Amos
     * 2024-06-13 14:58
     */
    public function onRedact($data)
    {
        $propertyInfo = $data['propertyInfo'];
        $userId = $data['userId'] ?? 0;

        /** @var PropertyLogRepository $propertyLogRepository */
        $propertyLogRepository = app()->make(PropertyLogRepository::class);
        $propertyLogData['property_id'] = $propertyInfo['id'];
        $propertyLogData['remark'] = '编辑物业: ' . $propertyInfo['code'];
        $propertyLogRepository->addData($propertyInfo['items_id'], $propertyLogData, $userId);
    }


    /**
     * 删除物业
     * @param $data
     * @return void
     *
     * @author Amos
     * 2024-06-13 14:55
     */
    public function onDelete($data)
    {
        $propertyInfo = $data['propertyInfo'];
        $userId = $data['userId'] ?? '0';

        ## 删除用户权限
        /** @var PropertyAuthAccessRepository $propertyAuthAccessRepository */
        $propertyAuthAccessRepository = app()->make(PropertyAuthAccessRepository::class);
        $propertyAuthAccessRepository->clearPropertyId($propertyInfo['id']);

        ## 删除物业日志
        /** @var PropertyLogRepository $propertyLogRepository */
        $propertyLogRepository = app()->make(PropertyLogRepository::class);
        $propertyLogRepository->delPropertyId($propertyInfo['id']);

        ## 删除关联标签
        /** @var PropertyBasicLabelRepository $propertyBasicLabelRepository */
        $propertyBasicLabelRepository = app()->make(PropertyBasicLabelRepository::class);
        $propertyBasicLabelRepository->deleteByPropertyId($propertyInfo['id']);

        # 清除能源设备
        /** @var EnergyMeterRepository $energyMeterRepository */
        $energyMeterRepository = app()->make(EnergyMeterRepository::class);
        $energyMeterRepository->unbindPropertyIdEnergyMeterId($propertyInfo['id']);
    }


    public function subscribe(Event $event)
    {
        $event->listen('property.create', [$this, 'onCreate']);## 创建物业
        $event->listen('property.redact', [$this, 'onRedact']); ## 物业编辑
        $event->listen('property.delete', [$this, 'onDelete']);## 物业删除
    }
}